package com.situ.blog.controller;

import com.situ.blog.pojo.User;
import com.situ.blog.service.IUserService;
import com.situ.blog.util.JSONResult;
import com.situ.blog.util.LayUITableJSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    //@Autowired
    //private UserMapper userMapper;

    @Autowired
    private IUserService userService;

    @RequestMapping("/add")
    @ResponseBody
    public JSONResult add(User user) {
        return userService.add(user);
    }

    @RequestMapping("/getUserAddPage")
    public String getUserAddPage() {
        return "/WEB-INF/user_add.jsp";
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public JSONResult deleteById(Integer id) {
        return userService.deleteById(id);
    }

    @RequestMapping("/getUserListPage")
    public String getUserListPage() {
        return "/WEB-INF/user_list.jsp";
    }

    // http://localhost:8081/user/selectByPage?page=1&limit=10
    @RequestMapping("/selectByPage")
    @ResponseBody
    public LayUITableJSONResult selectByPage(Integer page, Integer limit) {
        LayUITableJSONResult layUITableJSONResult = userService.selectByPage(page, limit);
        return layUITableJSONResult;
    }

    @RequestMapping("/selectAll")
    public String selectAll(Model model) {
        System.out.println("UserController.selectAll");
        List<User> list = userService.selectAll();
        //把数据放到Model内存里面
        model.addAttribute("list", list);
        //转发到user_list.jsp页面展示
        return "/WEB-INF/user_list.jsp";
    }

    // http://localhost:8080/user/selectAll2
    // /user/selectAll
    @RequestMapping("/selectAll2")
    @ResponseBody
    public List<User> selectAll2() {
        System.out.println("UserController.selectAll2");

        /*User user1 = new User();
        user1.setId(1);
        user1.setName("张三");
        user1.setPassword("124");
        User user2 = new User();
        user2.setId(2);
        user2.setName("李四");
        user2.setPassword("124");

        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);*/

        List<User> list = userService.selectAll();
        return list;
    }
}
